package server

import (
	"io/fs"
	"path/filepath"
	"stforum/global"
	"strings"

	"github.com/gin-gonic/gin"
)

// BindStaticRouter 将静态文件绑定到server上
func BindStaticRouter(static_path string, server *gin.Engine) {
	filepath.Walk(static_path, func(path string, info fs.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		tobind := strings.ReplaceAll(path, static_path, "")
		if strings.Count(tobind, "\\") != 0 {
			tobind = strings.ReplaceAll(tobind, "\\", "/")
		}
		global.Logger.Debug("正在绑定", tobind)
		if tobind == "/index.html" {
			server.StaticFile("/", path)
			return nil
		}
		server.StaticFile(tobind, path)
		return nil
	})

}
